package linkedlist.leetcode;

import java.util.Objects;

/**
 * @author lynn
 * @date 18-12-15 上午10:12
 */
public class ListNode {

    int val;
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int[] arr) {
        if (arr == null || arr.length == 0) {
            throw new IllegalArgumentException("array can not be empty");
        }

        this.val = arr[0];
        ListNode cur = this;
        for (int i = 1; i < arr.length; i++) {
            cur.next = new ListNode(arr[i]);
            cur = cur.next;
        }

    }


    @Override
    public String toString() {

        StringBuilder res = new StringBuilder();

        ListNode cur = this;
        while (Objects.nonNull(cur)) {
            res.append(cur.val).append("->");
            cur = cur.next;
        }
        res.append("NULL");
        return res.toString();
    }
}
